﻿using AutoMapper;
using B.S.Backtrack.Domain.Entities;
using B.S.Backtrack.ErrorCode;
using B.S.Backtrack.Infrastructure.Interface;
using B.S.Backtrack.Write.API.Application.Command.Image;
using MediatR;

namespace B.S.Backtrack.Write.API.Application.CommandHandler.Image
{
    public class CreateImageHandler : IRequestHandler<CreateImageCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Picture> piRep;
        private readonly IMapper mapper;

        public CreateImageHandler(IBaseRepository<Picture> piRep,IMapper mapper)
        {
            this.piRep = piRep;
            this.mapper = mapper;
        }

        /// <summary>
        /// 方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(CreateImageCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model=mapper.Map<Picture>(request);
           
            model.IsDel=false;
            piRep.Add(model);
            result.Code = ApiEnums.添加成功;
            result.Msg = ApiEnums.添加成功.ToString();
            result.Data = 1;
            return Task.FromResult(result);
        }
    }
}
